package com.cupcorn.balapkarung.entity;

import com.cupcorn.balapkarung.GameDefine;
import com.cupcorn.lib.Utils;
import com.cupcorn.lib.define.iDefines;
import com.cupcorn.lib.game.GameEngine;
import com.cupcorn.lib.game.GameRectangle;

public class World
{
	final float		GRAVITATION	= 5;
	float			curGravitation;
	
	boolean			isCollition;
	
	GameEngine		engine;
	
	MC				mc;
	GameRectangle[]	recGround;
	GameRectangle[]	recGameOver;
	
	public World(GameEngine engine, MC mc, Map map)
	{
		this.engine = engine;
		this.mc = mc;
		this.recGround = map.ground;
		this.recGameOver = map.gameOver;
	}
	
	public void onUpdate(float deltaTime)
	{
		isCollition = false;
		
		// if (mc.Y_JUMP_SPEED < curGravitation)
		// {
		// Utils.TRACE("j");
		// mc.isJumpDown = true;
		// }
		
		for (int i = 0; i < recGameOver.length; i++)
		{
			if (mc.mc.collidesWith(recGameOver[i]))
			{
				Utils.TRACE("switch");
				engine.nextState(GameDefine.STATE_GAME_OVER, iDefines.MESSAGE_STATE_INIT);
				mc.switchState(mc.DIE);
				break;
			}
		}
		
		for (int i = 0; i < recGround.length; i++)
		{
			if (mc.mc.collidesWith(recGround[i]))
			{
				isCollition = true;
				curGravitation = 0;
				
				mc.mc.setPosition(mc.mc.getX(), recGround[i].getY() - mc.mc.getHeight() + 2);
				mc.switchState(mc.MOVE);
			}
		}
		
		if (!isCollition)
		{
			curGravitation += GRAVITATION * deltaTime;
			
			mc.mc.setPosition(mc.mc.getX(), mc.mc.getY() + curGravitation);
		}
	}
}
